package de.tabuma.pm.project.view;

import java.io.*;
import java.util.*;
import java.util.logging.*;
import de.tabuma.lib.*;
import javax.servlet.*;
import javax.servlet.http.*;
import de.tabuma.pm.constants;
import de.tabuma.pm.project.entity.*;
import de.tabuma.customer.entity.*;
import de.tabuma.pm.projectnavigation.entity.*;

/**
 *
 * @author alefen
 */
public class update extends tabumaview {

    private static Logger log = Logger.getLogger(update.class.getName());

    public update(String servlet, Hashtable hashtable) {
        super(servlet, hashtable);
    }

    @Override
    public void processRequest(ServletContext context, HttpServletRequest request, HttpServletResponse response, PrintWriter out) {


        Project project = (Project) request.getAttribute(constants.paramProject);
        List<Customer> cList = (List<Customer>) request.getAttribute(constants.customerList);

        String projectName = "project name";
        String projectCustomer = "customer name";
        String projectDescription = "description";
        String projectStartpage = "startpage";

        String updateProject = "Update project";
        String update = "update";
        String toList = "back to project list";
        String noStartpage = "no startpage";

        out.println("<form action=\""+this.getServlet()+"\">");
        out.println("\t<div class=\"contentContent\">");
        out.println("\t\t<div class=\"contentBoxed\">");
        out.println("\t\t\t<div class=\"contentHeader\">"+updateProject+"</div>");
        out.println("\t\t\t<div class=\"contentContent\">");
        out.println("\t\t\t\t<div class=\"col1\">");
        out.println("\t\t\t\t\t<div class=\"col1_head\">"+projectName+"</div>");
        out.println("\t\t\t\t\t<div class=\"col1_content\"><input type=\"text\" name=\""+constants.paramProjectName+"\" value=\""+project.getName()+"\"/></div>");
        out.println("\t\t\t\t</div>");
        out.println("\t\t\t\t<div class=\"col2\">");
        out.println("\t\t\t\t\t<div class=\"col2_head\">"+projectCustomer+"</div>");
        out.println("\t\t\t\t\t<div class=\"col2_content\"><select name=\""+constants.paramProjectCustomerId+"\">");
        for(int i=0;i<cList.size();i++) {
            if(cList.get(i).getId()==project.getCustomerId()) {
                out.println("<option value=\""+cList.get(i).getId()+"\" selected>"+cList.get(i).getName()+"</option>");
            }
            else {
                out.println("<option value=\""+cList.get(i).getId()+"\">"+cList.get(i).getName()+"</option>");
            }
        }
        out.println("\t\t\t\t\t</select></div>");
        out.println("\t\t\t\t</div>");
        out.println("\t\t\t\t<div class=\"col1\">");
        out.println("\t\t\t\t\t<div class=\"col1_head\">"+projectDescription+"</div>");
        out.println("\t\t\t\t\t<div class=\"col1_content\"><textarea name=\""+constants.paramProjectDescription+"\" cols=\"20\" rows=\"2\">"+project.getDescription()+"</textarea></div>");
        out.println("\t\t\t\t</div>");
        out.println("\t\t\t\t<div class=\"col2\">");
        out.println("\t\t\t\t\t<div class=\"col2_head\">"+projectStartpage+"</div>");
        out.println("\t\t\t\t\t<div class=\"col2_content\">");
        out.println("\t\t\t\t\t\t<select name=\""+constants.paramProjectStartpage+"\">");
        out.println("\t\t\t\t\t\t\t<option value=\"0\">"+noStartpage+"</option>");
        List<ProjectNavigation> pnList = (List<ProjectNavigation>) request.getAttribute(constants.paramProjectStartpageList);
        for(int i=0;i<pnList.size();i++) {
            ProjectNavigation pn = pnList.get(i);
            if(pn.getId().intValue()==project.getStartPageId().intValue()) {
                out.println("\t\t\t\t\t\t\t<option value=\""+pn.getId().intValue()+"\" selected>"+pn.getName()+"</option>");
            }
            else {
                out.println("\t\t\t\t\t\t\t<option value=\""+pn.getId().intValue()+"\">"+pn.getName()+"</option>");
            }
        }
        out.println("\t\t\t\t\t\t</select>");
        out.println("\t\t\t\t\t</div>");
        out.println("\t\t\t\t</div>");
        out.println("\t\t\t\t<div class=\"col1\"></div>");
        out.println("\t\t\t\t<div class=\"col2\"></div>");
        out.println("\t\t\t</div>");
        out.println("\t\t\t<div class=\"contentContent\">");
        this.getParameter().put(constants.projectId, ""+project.getId());
        this.getParameter().put(constants.paramAction, ""+constants.ACTION_OVERVIEW);
        out.println(this.getHiddenAsAttribute());
        out.println("\t\t\t\t<input type=\"hidden\" name=\""+constants.paramProjectUpdate+"\" value=\"true\"/>");
        out.println("\t\t\t\t<input type=\"submit\" class=\"button\" value=\""+update+"\"/>");

        out.println("\t\t\t</div>");
        out.println("\t\t</div>");
        out.println("\t</div>");
        out.println("</form>");
    }


}
